package com.loanapp.server.user.provider.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.utils.MD5Util;
import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.database.common.base.SqlMapper;
import com.loanapp.server.user.api.user.entity.User;
import com.loanapp.server.user.api.user.query.UserQuery;
import com.loanapp.server.user.provider.mapper.UserMapper;
import com.loanapp.server.user.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper mapper;

    @Autowired
    SqlMapper sqlMapper;


    public Long Insert(User user) throws BusinessException {
        try {
            user.setPassword(MD5Util.md5(user.getPassword()));
            Integer rs= mapper.insert(user);
            return rs>0?user.getId():null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean DeleteByPk(Long aLong) throws BusinessException {
        return false;
    }

    public boolean Update(User user) throws BusinessException {
        try {
            user.setPassword(MD5Util.md5(user.getPassword()));
            Integer rs=mapper.updateById(user);
            return rs>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public User selectByPk(Long aLong) throws BusinessException {
        return mapper.selectById(aLong);
    }

    public User selectByModel(UserQuery userQuery) throws BusinessException {
        Map<String,Object> params=new HashMap();
        String sql="select * from loan_user where 1=1";
        if(userQuery!=null){
            if(StringUtil.isNotEmpty(userQuery.getPhone())){
                    sql+=" and phone=#{phone}";
                    params.put("phone",userQuery.getPhone());
            }
        }
        return sqlMapper.selectOne(sql,params,User.class);
    }

    public User selectBySelf(User user) throws BusinessException {
        return mapper.selectOne(user);
    }

    public List<User> selectListByModel(UserQuery userQuery) throws BusinessException {
        return null;
    }

    public Long selectCountByQueryMap(UserQuery userQuery) {
        return null;
    }

    public List<User> selectPageListByQueryMap(UserQuery userQuery) throws BusinessException {
        PageHelper.startPage(5,5);
        Map<String,Object> map=new HashMap<>();
        map.put("phone","15972132723");
        List<User> data = mapper.selectByMap(map);
        Pagination a=PageHelper.getPagination();


        return data;
    }

    @Override
    public Page<User> selectPageList(UserQuery userQuery) throws BusinessException {
        PageHelper.startPage(5,5);
        Map<String,Object> map=new HashMap<>();
        map.put("phone","15972132723");
        List<User> data = mapper.selectByMap(map);
        Pagination a=PageHelper.getPagination();
        return null;
    }
}
